package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.Province;
import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.exceptions.ProvinceException;
import com.yoyo.itravel.service.IProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.yoyo.itravel.utils.JSONUtils.writeValueToResponse;

/**
 * @Description 景点主题服务端
 *
 * @author HZX
 * @date 2020/5/11 22:59
 */
@Controller
@RequestMapping("/province")
public class ProvinceController {

    // 声明景点主题服务层接口对象
    @Autowired
    private IProvinceService provinceService;

    /**
     * @param requset
     * @param response
     * @return void
     * @Description 获得所有的省份
     */
    @RequestMapping("/findAllProvince")
    protected void findAllProvince(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            // 获得所有的省份
            List<Province> provinces = provinceService.findAllProvince();
            // 未出现异常，查找所有省份成功
            writeValueToResponse(provinces, response);
        } catch (ProvinceException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            writeValueToResponse(info, response);
        }
    }
}
